import requests
import re
from bs4 import BeautifulSoup

url = "http://wjw.beijing.gov.cn/"
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"}

html = requests.get(url , headers = headers).text

soup = BeautifulSoup(html,"lxml")

news_path1 = soup.find_all("div",{"class":"main_con_weinei_syx"})[1]

news_path2 = news_path1.find("div",{"class":"weinei_left_syx"})
news_path3 = news_path2.find("div",{"class":"wenhua_left_consy_syx"})

news_tags = news_path3.find_all("div",{"class":"wenhua_left_consy_line_text_syx"})

times_tag = news_path3.find_all("div",{"class":"wenhua_left_consy_line_date_syx"})
for time_tag1 in times_tag:
    time = time_tag1.get_text()

lists3 = []

for news_tag1 in news_tags:
    news = news_tag1.get_text()
    if (u"新增") in news:
        new_tag = news_tag1.find("a")["href"]
        new_tag = new_tag[2:len(new_tag)]
        new_url = url + new_tag
        lists3.append(new_url)


global data_year, data_add1
data_year1 = ""
data_add1 = ""
for list3 in lists3:
    html2 = requests.get(list3 , headers = headers).text
    soup2 = BeautifulSoup(html2, "lxml")
    data_tag1 = soup2.find("div",{"id":"zoom"})
    data = data_tag1.get_text()
    data2 = data.split("        ")
    for data in data2:
        if (u"无新增确诊病例") in data:
            data_add1 = data
            break
    if data_add1 !="":
        break
#print(data_add1)

for list3 in lists3:
    html2 = requests.get(list3 , headers = headers).text
    soup2 = BeautifulSoup(html2, "lxml")
    data_tag1 = soup2.find("div",{"id":"zoom"})
    data = data_tag1.get_text()
    data2 = data.split("        ")
    for data in data2:
        if(u"男性病例") in data:
            data_year1 = data
            break
    
    if data_year1 != "":
        break

data_add2 = data_add1.split("，")
global flag
flag = 0
for data_add3 in data_add2:
    if (u"石景山区") in data_add3:
        data_add4 = re.sub("\D","",data_add3)
        data_add = "石景山区连续" + data_add4 + "日无增长"
        flag = 1
        break
if flag == 0:
    data_add = "石景山区14日内有增长"

#print(data_add)

#436例确诊病例中，男性病例206例，占47.2%，女性病例230例，占52.8%；年龄范围为6个月～94岁，其中5岁以下14例，占3.2%，6岁至17岁17例，占3.9%，18岁至59岁294例，占67.4%，60岁及以上111例，占25.5%。

labels1 = []
values1 = []
data_year2 = data_year1.split("；")
data_year3 = data_year2[0]
data_year4 = data_year3.split("，")
for data_year5 in data_year4:
    if (u"男性病例")in data_year5:
        values1.append(re.sub("\D","",data_year5))
        labels1.append("男性病例"+str(re.sub("\D","",data_year5))+"例")
    elif (u"女性病例") in data_year5:
        values1.append(re.sub("\D","",data_year5))
        labels1.append("女性病例"+str(re.sub("\D","",data_year5))+"例")


values2 = []
data_year6 = data_year2[1]
data_year7 = data_year6.split("，")
for data_year8 in data_year7:
    if (u"例") in data_year8:
        if (u"以下") in data_year8:
            values2.append(re.sub("\D","",data_year8.split("以下")[1]))
        elif (u"至") in data_year8:
            values2.append(re.sub("\D","",data_year8.split("岁")[2]))
        elif (u"以上") in data_year8:
            values2.append(re.sub("\D","",data_year8.split("以上")[1]))


print(values2)


#做两个饼状图（年龄，性别）
#找出石景山区和北京市增长数据做弹出窗）


